package com.ychug.gateway.system;

import com.ychug.common.base.ResultAjaxHash;
import com.ychug.common.utils.StringUtils;
import com.ychug.framework.business.service.SysLoginService;
import com.ychug.framework.web.base.BaseController;
import com.ychug.framework.web.page.TableDataInfo;
import com.ychug.service.system.entity.SysUserEntity;
import com.ychug.service.system.service.ISysDeptService;
import com.ychug.service.system.service.ISysPostService;
import com.ychug.service.system.service.ISysRoleService;
import com.ychug.service.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/sys/user")
public class SysUserController extends BaseController
{
    private String prefix = "modules/sys/user";

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysPostService postService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private SysLoginService sysLoginService;


    @GetMapping()
    public String user()
    {
        return prefix + "/viewUser";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysUserEntity user)
    {
        startPage();
        List<SysUserEntity> list = userService.selectUserList(user);
        return getDataTable(list);
    }


    @PostMapping("/export")
    @ResponseBody
    public ResultAjaxHash export(SysUserEntity user)
    {
        List<SysUserEntity> list = userService.selectUserList(user);

       return null ;// return utils.exportExcel(list, "user");
    }

    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("roles", roleService.selectRoleAll());
        mmap.put("posts", postService.selectPostAll());
        return prefix + "/addUser";
    }

    /**
     * 新增保存用户
     */

    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public ResultAjaxHash addSave(SysUserEntity user)
    {
        if (StringUtils.isNotNull(user.getUserId()) && SysUserEntity.isAdmin(user.getUserId()))
        {
            return error("不允许修改超级管理员用户");
        }
        user.setSalt(user.getLoginName());
        //user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
        String newPass = sysLoginService.getNewPassword(user.getPassword());
        user.setPassword(newPass);
        user.setCreateBy(user.getLoginName());

        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
    {
       SysUserEntity sysUserEntity =  userService.selectUserById(userId);
        mmap.put("user", sysUserEntity);
        mmap.put("roles", roleService.selectRolesByUserId(userId));
        mmap.put("posts", postService.selectPostsByUserId(userId));
        mmap.put("dept", deptService.selectDeptById(sysUserEntity.getDeptId()));
        return prefix + "/editUser";
    }

    /**
     * 修改保存用户
     */

    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public ResultAjaxHash editSave(SysUserEntity user)
    {
        if (StringUtils.isNotNull(user.getUserId()) && SysUserEntity.isAdmin(user.getUserId()))
        {
            return error("不允许修改超级管理员用户");
        }
        user.setUpdateBy("admin");
        return toAjax(userService.updateUser(user));
    }


    @GetMapping("/resetPwd/{userId}")
    public String resetPwd(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        mmap.put("user", userService.selectUserById(userId));
        return prefix + "/resetPwd";
    }


    @PostMapping("/resetPwd")
    @ResponseBody
    public ResultAjaxHash resetPwdSave(SysUserEntity user)
    {
       // user.setSalt(ShiroUtils.randomSalt());
     //   user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
        String newPass = sysLoginService.getNewPassword(user.getPassword());
        user.setPassword(newPass);
        return toAjax(userService.resetUserPwd(user));
    }


    @PostMapping("/remove")
    @ResponseBody
    public ResultAjaxHash remove(String ids)
    {
        try
        {
            return toAjax(userService.deleteUserByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 校验用户名
     */
    @PostMapping("/checkLoginNameUnique")
    @ResponseBody
    public String checkLoginNameUnique(SysUserEntity user)
    {
        return userService.checkLoginNameUnique(user.getLoginName());
    }

    /**
     * 校验手机号码
     */
    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public String checkPhoneUnique(SysUserEntity user)
    {
        return userService.checkPhoneUnique(user);
    }

    /**
     * 校验email邮箱
     */
    @PostMapping("/checkEmailUnique")
    @ResponseBody
    public String checkEmailUnique(SysUserEntity user)
    {
        return userService.checkEmailUnique(user);
    }
}